/*
 * @(#)MovieService.java 2013-9-22
 * 
 * Copy Right@ 纽海信息技术有限公司
 */ 

package service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;

import bean.Movie;
import bean.Student;


/**
 * <pre>
 * @author zhangtengfei
 *
 *
 * 创建日期: 2013-9-22
 * 修改人 :  
 * 修改说明: 
 * 评审人 ：
 * </pre>
 */
@Path("/movie")
public class MovieService {

    @GET
    @Path("/getXml")
    @Produces(MediaType.APPLICATION_XML)
    public Movie getMovieXml(){
        Movie movie = new Movie();
        movie.setId(1);
        movie.setTitle("movie");
        return movie;
    }
    @PUT
    @Path("/putXml")
    @Produces(MediaType.APPLICATION_XML)
    public Movie putMovieXml(){
        Movie movie = new Movie();
        movie.setId(1);
        movie.setTitle("movie");
        return movie;
    }
    @POST
    @Path("/postXml")
    @Produces(MediaType.APPLICATION_XML)
    public List<Movie> postMovieXml(){
        List<Movie> movies = new ArrayList<Movie>();
        Movie movie = new Movie();
        movie.setId(1);
        movie.setTitle("movie");
        movies.add(movie);
        return movies;
    }
    @GET
    @Path("/getJson")
    @Produces(MediaType.APPLICATION_JSON)
    public Movie getMovieJson(){
        Movie movie = new Movie();
        movie.setId(1);
        movie.setTitle("movie");
        return movie;
    }
    @GET
    @Path("/getTxt")
    @Produces(MediaType.TEXT_PLAIN)
    public Movie getMovieTxt(){
        Movie movie = new Movie();
        movie.setId(1);
        movie.setTitle("movie");
        return movie;
    }
    
    
    @POST
    @Path("/postXmlReturnJson")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_XML)
    public List<Movie> postMovieXmlReturnXml(List<Movie> movies){
        return movies;
    }
    
    @POST
    @Path("/pathParam/{id}/{title}")
    @Produces(MediaType.APPLICATION_XML)
    public Movie pathParam(@PathParam("id")int id,@PathParam("title") String title) {
        Movie movie = new Movie();
        movie.setId(id);
        movie.setTitle(title);
        return movie;
    }
    /**
     * 正则表达式
     * @param id
     * @param title
     * @return
     */
    @POST
    @Path("/pathParamRegular/aa-{title:b+}/{note:.*}")
    @Produces(MediaType.APPLICATION_XML)
    public Movie pathParamRegular(@PathParam("title")String title,@PathParam("note") String note) {
        Movie movie = new Movie();
        movie.setTitle(title);
        movie.setNote(note);
        return movie;
    }
    
    @POST
    @Path("/queryParam")
    @Produces(MediaType.APPLICATION_XML)
    public Movie queryParam(@QueryParam("param") String param) {
        Movie movie = new Movie();
        movie.setId(1);
        movie.setTitle(param);
        return movie;
    }
    
    @POST
    @Path("/formParam")
    @Produces(MediaType.APPLICATION_XML)
    public Student formParam(@FormParam("name") String name,@FormParam("age") int age) {
        Student student = new Student();
        student.setName(name);
        student.setAge(age);
        System.out.println(student);
        return student;
    }
    
    @POST
    @Path("/headerParam")
    @Produces(MediaType.APPLICATION_XML)
    public Student headerParam(@HeaderParam("name") String name,@HeaderParam("age") int age) {
        Student student = new Student();
        student.setName(name);
        student.setAge(age);
        System.out.println(student);
        return student;
    }
    
    @POST
    @Path("/headerParam2")
    @Produces(MediaType.APPLICATION_XML)
    public Student headerParam2(@Context HttpHeaders headers) {
        Student student = new Student();
        student.setName(headers.getRequestHeader("name").get(0));
        student.setAge(Integer.valueOf(headers.getRequestHeader("age").get(0)));
        System.out.println(student);
        return student;
    }
    
    @POST
    @Path("/matrixParam/{id}")
    @Produces(MediaType.APPLICATION_XML)
    public Movie matrixParam(@PathParam("id")int id,@MatrixParam("title") String title) {
        Movie movie = new Movie();
        movie.setId(id);
        movie.setTitle(title);
        return movie;
    }
    
    @POST
    @Path("/cookieParam")
    @Produces(MediaType.APPLICATION_XML)
    public Movie cookieParam(@Context HttpHeaders headers) {
        Map cookie = headers.getCookies();
        System.out.println(cookie);
        Movie movie = new Movie();
//        movie.setId(id);
//        movie.setTitle(title);
        return movie;
    }
}
